#include <string>
#include <fstream>

#include "primeFunctions.hpp"
#include "json11.hpp"

using namespace std;

yjy::Config::Config(std::string fileAddress)
{
	string fileContent;
	string err;

	ifstream ifs(fileAddress);
	fileContent.assign( (istreambuf_iterator<char>(ifs)), (istreambuf_iterator<char>()));

	json11::Json icf_json = json11::Json::parse(fileContent, err);
	
    this->run_criminisi = icf_json["criminisi"].bool_value();
    this->run_improved = icf_json["improved"].bool_value();
    this->variable_init_ws = icf_json["VIWS"].bool_value();
    this->init_ws = icf_json["initWS"].int_value();
    
    this->icf.dynamicWS = icf_json["improved_config"]["dynamicWS"].bool_value();
    this->icf.new_priority = icf_json["improved_config"]["newPriority"].bool_value();
    this->icf.isQuikMatch = icf_json["improved_config"]["quikMatch"].bool_value();    
    this->icf.candidatorNum = icf_json["improved_config"]["candidatorNum"].int_value();
    this->icf.CIE_color = icf_json["improved_config"]["cie"].int_value();
    this->icf.CIE_color_inverse = icf_json["improved_config"]["CIE_color_inverse"].int_value();    
    
}

double yjy::norm_yjy(cv::Mat &src1, cv::Mat &src2, int normType, cv::Mat &mask)
{
    float deta, ssd;
    cv::Vec3b a, b;
    cv::Mat diff(src1.rows, src1.cols, CV_32FC1, cv::Scalar(0));
    cv::Mat zero(src1.rows, src1.cols, CV_32FC1, cv::Scalar(0));

    for (int i = 0; i < src1.rows; i++)
    {
        for (int j = 0; j < src1.cols; j++)
        {
            if (mask.at<float>(i, j) == 0)
                continue;
            a = src1.at<cv::Vec3b>(i, j);
            b = src2.at<cv::Vec3b>(i, j);
            deta = sqrt(pow(a[0] - b[0], 2) + pow(a[1] - b[1], 2) + pow(a[2] - b[2], 2));
            diff.at<float>(i, j) = deta;
        }
    }

    ssd = cv::norm(diff, normType);
    return ssd;
}